﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DetetiveKGB.Models;

namespace DetetiveKGB.Controllers
{
    public class UsersController : Controller
    {
        //
        // GET: /User/
        
        public ActionResult Index()
        {
            List<UserModel> list = UserModel.ListAll();
            return View(list);
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(UserModel model)
        {
            int result = -1;
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    result = model.Add();
                    if (result < 0)
                    {
                        ModelState.AddModelError("", "Erro na inclusão de usuário");
                        return View(model);
                    }
                    else
                    {
                        model.AddUser(result);
                    }
                }
                else
                    return View(model);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /User/Edit/5
 
        public ActionResult Edit(int id)
        {
            UserModel u = UserModel.Get(id);
            return View(u);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, UserModel model)
        {
            int result = -1;
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    if (id > 0)
                    {
                        model.LoginId = id;
                        model.Update();
                        int userId = UserModel.Get(id).UserId;
                        if (userId == 0)
                            model.AddUser(id);
                        else
                            model.UpdateUser(id);
                    }
                    else
                    {
                        result = model.Add();
                        if (result < 0)
                        {
                            ModelState.AddModelError("", "Erro na inclusão de usuário");
                            return View(model);
                        }
                        else
                        {
                            model.AddUser(result);
                        }
                    }
                }
                else
                    return View(model);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /User/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
